package site.shenjie.hystrix.feign.demo.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.shenjie.hystrix.feign.demo.client.CallServiceClient;

@RestController
public class HystrixFeignController {
    @Autowired
    private CallServiceClient callServiceClient;

    /**
     * 注解：@HystrixCommand配置HystrixCommand，相当于一个hystrix-demo中的MyHystrixCommand类
     * 其中：
     * 1.fallbackMethod指定调用失败时的处理方式
     * 2.commandProperties指定command的其他配置信息
     * 配置信息类似于一个Map，其中name指定配置项是什么，value指定该项配置的值是什么，
     * 比如：
     * name="execution.isolation.strategy"——指定隔离策略 : value="THREAD"——使用线程隔离
     * name="requestCache.enabled"——指定是否要开启缓存功能 : value="false"——不开启缓存
     */
    @RequestMapping("/call/welcome")
    @HystrixCommand(
            fallbackMethod = "defaultCallHello"
            , commandProperties =
            {
                    @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD")
                    , @HystrixProperty(name = "requestCache.enabled", value = "true")
            }
    )
    public String callWelcome(String name) {
        String result = callServiceClient.welcome(name);
        return result;
    }

    public String defaultCallHello(String name) {
        return "Hello " + name;
    }
}
